package com.zzmcc.wechat.action;

import com.zzmcc.wechat.WeChat;
import com.zzmcc.wechat.entity.Account;
import com.zzmcc.wechat.service.AccountService;
import com.zzmcc.wechat.service.MenuService;
import com.zzmcc.wechat.service.WechatService;
import com.zzmcc.wechat.util.HttpKit;
import com.zzmcc.wechat.util.Tools;
import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.mvc.DataContext;
import org.smart4j.framework.mvc.annotation.Action;
import org.smart4j.framework.mvc.annotation.Request;
import org.smart4j.framework.mvc.bean.Params;
import org.smart4j.framework.mvc.bean.Result;
import org.smart4j.framework.mvc.bean.View;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.StringUtil;
import org.smart4j.plugin.security.SecurityHelper;

import java.util.List;
import java.util.Map;

@Action
public class AccountAction {

    @Inject
    private AccountService accountService;

    @Inject
    private WechatService wechatService;

    @Inject
    private MenuService menuService;

    @Request.Get("/accounts")
    public View accounts(){
        String keyword="";
        List<Account> accountList = accountService.getAccounts(keyword);
        DataContext.Request.put("accountList",accountList);
        return  new View("account/index.jsp");
    }
    @Request.Post("/accounts")
    public  View search(Params params){
        String keyword =params.getString("account");
        List<Account> accountList = accountService.getAccounts(keyword);
        DataContext.Request.put("accountList",accountList);
        return  new View("account/list.jsp");
    }
    @Request.Get("/account")
    public View create(){
        return  new View("account/create.jsp");
    }

    @Request.Post("/account")
    public Result save(Params params){
        Map<String,Object> fieldMap = params.getFieldMap();
        boolean result=accountService.saveAccount(fieldMap);
        return new Result(result);
    }
    @Request.Delete("/account/{id}")
     public Result delete(long id){
        boolean result = accountService.delAccount(id);
        return new Result(result);
     }

    @Request.Get("/account/{id}")
    public  View manage(long id){
        Account account = accountService.findAccount(id);
        DataContext.Request.put("account",account);
        return  new View("account/manage.jsp");
    }
    @Request.Put("/account/{id}")
    public  Result update (long id,Params params){
        Map<String,Object> fieldMap =params.getFieldMap();
        boolean result = accountService.updateAccount(id,fieldMap);
        return  new Result(result);
    }
    /**
     * 查询菜单
     */
    @Request.Get("/account/menu/{id}")
    public Result getMenu(long id){
        Account account = accountService.findAccount(id);
        String appid=account.getAppid();
        String secret=account.getAppsecret();
        if (StringUtil.isNotEmpty(appid)&&StringUtil.isNotEmpty(secret)){
            String accessToken= wechatService.getAccessToken(appid,secret);
            Map<String,Object> menuMap = menuService.getMenuInfo(accessToken);
            String menuStr= CastUtil.castString(menuMap.get("menu"));
            return  new Result(true).data(menuStr);
        }
        else
            return  new Result(false);
    }
    /**
     * 创建菜单
     */
    @Request.Post("/account/menu/{id}")
    public Result saveMenu(long id,Params params)
    {
        String menuJson = params.getString("message");
        Account account = accountService.findAccount(id);
        String appid=account.getAppid();
        String secret=account.getAppsecret();
        if (StringUtil.isNotEmpty(appid)&&StringUtil.isNotEmpty(secret)){
            String accessToken= wechatService.getAccessToken(appid,secret);
            boolean result = menuService.createMenu(accessToken,menuJson);
            return  new Result(result);
        }
        else
            return  new Result(false);

    }
    /**
     * 删除自定义菜单
     */
    @Request.Delete("/account/menu/{id}")
    public Result deleteMenu(long id){
        Account account = accountService.findAccount(id);
        String appid=account.getAppid();
        String secret=account.getAppsecret();
        if (StringUtil.isNotEmpty(appid)&&StringUtil.isNotEmpty(secret)){
            String accessToken= wechatService.getAccessToken(appid,secret);
            boolean result = menuService.deleteMenu(accessToken);
            return  new Result(result);
        }
        else
            return  new Result(false);

    }
    @Request.Get("/account/message/{msgType}")
    public  Result getMsg(String msgType){

        String message = Tools.getMsg(msgType);
        return new Result(true).data(message);

    }
}
